package com.boboPlanet.user.filter;

import com.boboPlanet.comm.context.TraceIdContext;
import com.boboPlanet.comm.enums.PlanetCommCode;
import com.boboPlanet.comm.thread.ThreadMdcUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.context.annotation.Configuration;


/**
 * @Description: TODO 解决MDC无法get问题 http://www.voycn.com/article/springbootopenfeignkaiqirongduanzhihoumdcweinulldelijie
 * @Author: mis_wu
 * @Date: 2023/5/7
 * @Company planet
 **/
@Configuration
@Slf4j
public class FeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {

        String traceId = ThreadMdcUtil.get();

        //不能用TraceIdContext获取，因为feign调用时当前线程变了
        String traceId2 = TraceIdContext.getTranceId();

        String thread = Thread.currentThread().getName();


        requestTemplate.header(PlanetCommCode.COMM_LOG_KEY.getCode(), traceId);
    }
}
